<?php 
define('IN_ADAGPS', TRUE);
include_once('adagps.com/cfg.php');
include_once(CORE_PATH.'includes/summary_functions.php');
include_once(CORE_PATH.'database/commonDB.php'); 
if(!ConnectDB()) {
    die('Không kết nói được DATABASE');
}

$sql = 'select service_id,LPAD(time_auto_send_sms,2,0) as timesms,phone_auto_send_sms as phonenumber from cus_config c
join tbl_service s on s.active = 1 and s.id= c.service_id
where c.flg_auto_send_sms = 1 and HOUR(now())=time_auto_send_sms  and datediff(s.expire_date,now())>0';  //and HOUR(now())=time_auto_send_sms

//echo $sql; die();
$array = $objDbUpdate->GetArray($sql); // lấy ra mãng dịch cần tổng hợp dữ liệu nhắn tin tự động
$count = count($array);
for($i=0;$i<$count;$i++){
    $service_id=$array[$i]['service_id'];
    $timesms=$array[$i]['timesms'];
    $phonenumber=$array[$i]['phonenumber'];
    CreateSMS_auto($service_id,$timesms,$phonenumber);
}



function CreateSMS_auto($service_id,$timesms,$phonenumber){
    global $objDbUpdate,$lang;     
        
    $date = date('Y-m-d')." $timesms:00:00";
    $timebuffer = 24*60*60; // ngày 24 giờ * 60 Phút * 60 giây
    $sql= "select sum(TIMESTAMPDIFF(MINUTE,timestart,timeend)) as timerent ,
    sum(num_voucher) as numvoucher,
    count(id) as numrent,sum(money_rent) as money_rent,sum(price_voucher) as money_voucher
    ,sum(total) as money_total,sum(arising_price) as money_arising
     from fm_current_book_$service_id WHERE
     flgcal=1  and  TIMESTAMPDIFF(second,timeend,'$date')<=$timebuffer and timeend < '$date' and flg_cancel = 0
    "; 
   // echo $sql; die();
    $arr =  $objDbUpdate->GetArray($sql);
    
    //$arr[0]['timerent'] = ConvertTimeToHM($arr[0]['timerent']);

    $money_voucher = $arr[0]['money_voucher'];
    $money_total = $arr[0]['money_total'];
    $money_arising = $arr[0]['money_arising'];
    $money_rent = $arr[0]['money_rent'];
    $arr[0]['money_product'] = $money_total - $money_rent+$money_voucher+$money_arising;
    $datesend = date('d.m.Y');
    $timesend = "$timesms:00:00";
    
    $money_get = 0;
    $money_pay =0;
    $sqlpay = "select sum(money) as money,flg from cus_pay where 
     TIMESTAMPDIFF(second,date_created,'$date')<=$timebuffer and date_created < '$date'
      and service_id = '$service_id' group by flg ";
    $arrPay = $objDbUpdate->GetArray($sqlpay);
    for($i=0;$i<count($arrPay);$i++){
        if($arrPay[$i]['flg']==1){
           $money_get = $arrPay[$i]['money']; 
        }else{
           $money_pay = $arrPay[$i]['money']; 
        }
    }
    
    $money_get=$money_get==''?0:$money_get;
    $money_pay=$money_pay==''?0:$money_pay;
    $money_revenue = $money_total - $money_pay + $money_get;
    
    /* Bắt đầu chuẩn bị phần này chuẩn bị cho phần tin nhắn*/
    $numrent      = FormatMoney($arr[0]['numrent']) ; 
    $timerent     = ConvertTimeToHMSMS($arr[0]['timerent']) ;
    $money_rent   = FormatMoney($money_rent) ; 
    $money_product= FormatMoney($arr[0]['money_product']) ; 
    $money_arising= FormatMoney($money_arising) ; 
    $numvoucher   = FormatMoney($arr[0]['numvoucher']) ; 
    $money_voucher= FormatMoney($money_voucher) ; 
    $money_pay    = FormatMoney($money_pay) ; 
    $money_get    = FormatMoney($money_get) ; 
    $money_revenue= FormatMoney($money_revenue) ;
   // echo '<pre>'; print_r($arr); die();
    $smscontent = SMS_COUNT_RENT.$numrent. SMS_SYSTEM_DOWNLINE.SMS_TIME_RENT.$timerent. 
    SMS_SYSTEM_DOWNLINE.SMS_MONEY_RENT.$money_rent.SMS_SYSTEM_DOWNLINE.SMS_MONEY_PRODUCT.
    $money_product. SMS_SYSTEM_DOWNLINE.SMS_MONEY_ARISING.$money_arising.SMS_SYSTEM_DOWNLINE.
    SMS_NUM_VOUCHER.$numvoucher.SMS_SYSTEM_DOWNLINE.SMS_MONEY_VOUCHER.$money_voucher.SMS_SYSTEM_DOWNLINE.
    SMS_MONEY_GET.$money_get.SMS_SYSTEM_DOWNLINE.SMS_MONEY_PAY.$money_pay.SMS_SYSTEM_DOWNLINE.SMS_MONEY_REVENUE.
    $money_revenue.SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_CAL_FROM.$timesend. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_DATE.$datesend; 
    
    $arrPhone = explode(';',$phonenumber);
    $num_phone = count($arrPhone);   $sql = "INSERT INTO tbl_sms(date_created, sms_type, sms_content, service_id, phone_number, num_phone, sms_timesend)
     values(now(),1,'$smscontent',$service_id,'$phonenumber',$num_phone,now())" ;
     $objDbUpdate->Execute($sql); 
    
    /* Kết thúc chuẩn bị phần này chuẩn bị cho phần tin nhắn*/    
   
}  

CloseDB();

/*function  summary($service_id,$timesms){
    global $objDbUpdate,$hotel_id;
    $date = date('Y-m-d')." $timesms:00:00";
    $timebuffer = 24*60*60; // ngày 24 giờ * 60 Phút * 60 giây
    $sql= "select sum(TIMESTAMPDIFF(MINUTE,timestart,timeend)) as timerent ,
    sum(num_voucher) as numvoucher,
    count(id) as numrent,sum(money_rent) as money_rent,sum(price_voucher) as money_voucher
    ,sum(total) as money_total,sum(arising_price) as money_arising
     from fm_current_book_$service_id WHERE
     flg=1  and  TIMESTAMPDIFF(second,timeend,'$date')<=$timebuffer and timeend < '$date' and flg_cancel = 0
    "; 
    $arr =  $objDbUpdate->GetArray($sql);
    return $arr;
} */

function FormatMoney($money){
    $len = strlen($money);
    $postInt = strpos($money,'.')===false?$len:strpos($money,'.');
    
    $arrAra=array();
    if($postInt<4) return $money;
    else{
        for($i=$postInt;$i>=3;$i=$i-3){
            $arrAra[] = substr($money,$i-3,$len);
            $money = substr($money,0,$i-3);
            $len=$i-3;
        }
        if(strlen($money)>0){
          $arrAra[]=  $money;
        }
        
    }
    $count = count($arrAra);
    $result = '';
    for($i=$count-1;$i>0;$i--){
        $result.=$arrAra[$i].',';
    }
    $result .=$arrAra[0];
    return $result;
}
?>
